﻿using System;
using System.Windows.Forms;
using KZ.Exception;
using KZ.Lottery.BLL;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery
{
    public partial class FrmResultInsert : Form
    {
        #region Private Variables

        private bool _IsSaved = false;

        #endregion Private Variables

        #region Private Methods

        private void FrmResultInsert_Load(object sender, EventArgs e)
        {
            dtpResultDate.Value = DateTime.Now;
            if (Action == KZ_Resource.Action_Update)
            {
                LoadResult();
            }
        }

        private void FrmResultInsert_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (_IsSaved)
            {
                FrmResult frm = Application.OpenForms["FrmResult"] as FrmResult;
                if (frm != null)
                {
                    frm.LoadResult();
                }
            }
        }

        private void LoadChannel(string no)
        {
            try
            {
                cmbChannelID.DataSource = ChannelBLL.GetChannelListNotResult(no, dtpResultDate.Value.Date);
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadChannel", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void LoadResult()
        {
            try
            {
                Result = ResultBLL.GetResult(ResultID);
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("LoadRule", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        private void dtpResultDate_ValueChanged(object sender, EventArgs e)
        {
            string dayOfWeek = dtpResultDate.Value.DayOfWeek.ToString();
            lblDate.Text = KZ_Resource.ResourceManager.GetString(dayOfWeek);
            LoadChannel(dayOfWeek);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                ResultBLL resultBLL = new ResultBLL();
                if (Action == KZ_Resource.Action_Insert)
                {
                    if (resultBLL.InsertResult(Result))
                    {
                        Global.Clear(this);
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RESULT_INSERT, true);
                        cmbChannelID.Focus();
                        _IsSaved = true;
                        dtpResultDate_ValueChanged(dtpResultDate, null);
                    }
                }
                else
                {
                    if (resultBLL.UpdateResult(Result))
                    {
                        Global.SetMessage(lblMessage, KZ_Message.BLM_SUC_RULE_UPDATE, true);
                        cmbChannelID.Focus();
                        _IsSaved = true;
                        dtpResultDate_ValueChanged(dtpResultDate, null);
                    }
                }
            }
            catch (DataAccessException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (BusinessLogicException ex)
            {
                Global.SetMessage(lblMessage, ex.Message, false);
            }
            catch (System.Exception ex)
            {
                Log.LogEx("btnSave_Click", ex);
                Global.SetMessage(lblMessage, KZ_Message.BLM_ERR_GENERAL, false);
            }
        }

        #endregion Private Methods

        #region Constructors

        public FrmResultInsert()
        {
            InitializeComponent();
            dtpResultDate.CustomFormat = Global.Culture.DateTimeFormat.ShortDatePattern;

            cmbChannelID.DisplayMember = ChannelColumns.ChannelName.ToString();
            cmbChannelID.ValueMember = ChannelColumns.ChannelID.ToString();
        }

        #endregion Constructors

        #region Public Members

        public string ResultID { get; set; }

        public string Action { get; set; }

        public Result Result
        {
            get
            {
                return new Result
                {
                    ResultID = Action == KZ_Resource.Action_Insert ? Guid.NewGuid().ToString() : ResultID,
                    ResultDate = dtpResultDate.Value,
                    ChannelID = Utility.StringParse(cmbChannelID.SelectedValue),
                    Head = txtHead.Text,
                    Tail = txtTail.Text,
                    Status = KZ_Resource.ResultStatus_Draft
                };
            }
            set
            {
                dtpResultDate.Value = value.ResultDate;
                cmbChannelID.SelectedValue = value.ChannelID;
                txtHead.Text = value.Head;
                txtTail.Text = value.Tail;
            }
        }

        #endregion Public Members

        private void Frm_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                btnSave_Click(btnSave, null);
            }
        }
    }
}